const path = require('path');
const htmlWebpackPlugin = require('html-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const webpack = require('webpack')
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
    entry: [
        './src/main.js',
        "core-js/modules/es6.promise",
        "core-js/modules/es6.array.iterator",
    ], //入口文件(要构建的文件)
    output: {
        filename: 'bundle.js', //构建输出文件名
        path: path.resolve(__dirname, 'dist') //构建输出文件夹
    },
    devServer: {  //web-dev-server配置
        open: true,
        port: 3000,
        // contentBase: path.join(__dirname, "dist"),
        hot: true,
        proxy: {
            '/api': {
                target: 'http://vue.studyit.io',
                changeOrigin: true,
            }
        }
    },
    plugins: [
        new htmlWebpackPlugin({ //自动追加 <script type="text/javascript" src="bundle.js"></script></body>到页面最后
            template: path.join(__dirname, './src/index.html'),//指定模板页面,
            filename: "index.html",//生成的页面名称
            title: "vue",
            minify: { //压缩html
                collapseWhitespace: true,
                removeComments: true,
                removeRedundantAttributes: true,
                removeScriptTypeAttributes: true,
                removeStyleLinkTypeAttributes: true,
                useShortDoctype: true
            }
        }),
        new CleanWebpackPlugin(), //打包前清理dist目录
        new VueLoaderPlugin(), //vue文件插件
        new MiniCssExtractPlugin({  //抽取压缩css
            filename: '[name].css',
            chunkFilename: '[id].css',
            ignoreOrder: false,
        }),
    ],
    optimization: {
        splitChunks: {
            chunks: 'async',
            minSize: 30000,
            maxSize: 0,
            minChunks: 1,
            maxAsyncRequests: 5,
            maxInitialRequests: 3,
            automaticNameDelimiter: '~',
            automaticNameMaxLength: 30,
            name: true,
            cacheGroups: {
                vendors: {
                    test: /[\\/]node_modules[\\/]/,
                    priority: -10
                },
                default: {
                    minChunks: 2,
                    priority: -20,
                    reuseExistingChunk: true
                }
            }
        }
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use:[


                    'css-loader',
                ]
            },
            {
                test: /\.scss$/,
                use: [
                    'style-loader',
                    'css-loader',
                    'sass-loader',
                ]
            },
            {
                test: /\.(png|svg|jpg|gif)$/,
                use: [
                    'file-loader?name=images/[hash:8]-[name].[ext]'
                ]
            },
            {
                test: /\.(woff|woff2|eot|ttf|otf)$/,
                use: [
                    'file-loader?name=font/[hash:8]-[name].[ext]'
                ]
            },
            {
                test: /\.vue$/,
                use: [
                    'vue-loader'
                ]
            },
            {
                test: /\.(js)$/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env'],
                        plugins: [require('@babel/plugin-proposal-class-properties')]
                    }
                },
                exclude: "/node_modules", //排除node_modules
            }
        ]
    },
    resolve: {
        //修改vue导入时的路径
        alias: {
            vue$: "vue/dist/vue.js"
        }
    }
};
